//
//  StatusReweeted.swift
//  sina
//
//  Created by 江先铨 on 15/11/17.
//  Copyright © 2015年 jxqstatic. All rights reserved.
//

import UIKit
import SnapKit

class StatusReweeted: UIView {
    
    //转发微博的底部约束, 用来记录底部约束  后续根据是否有配图, 来动态修改
    var bottomConstraint: Constraint?
    
    var retweetedStatus:  StatusModel? {
    
        didSet {
           //给标题赋值
           retweetedLabel.text = retweetedStatus?.text
            
            //需要把原来的约束给移除掉 不然的会产生复用的问题 会影响cell 的复用 原来的图片会被覆盖掉 所以需要把数据给移除掉
            self.bottomConstraint?.uninstall()
            //给配图图片赋值 在这里做了一个判断 当配图图片是不为nil 并且 数组是有值的情况下 给数组赋值
            if let picurls = retweetedStatus?.imageURLs where picurls.count > 0 {
                //给转发微博赋值
              pictureView.imageURLs = picurls
            //隐藏转发微博
            pictureView.hidden = false
            //设置约束 因为这是有配图视图的数据 所以需要对转发微博的数据的约束进行更新 
            self.snp_updateConstraints(closure: { (make) -> Void in
                    //有配图视图的约束 需要设置配图视图的约束
                    self.bottomConstraint =  make.bottom.equalTo(pictureView.snp_bottom).offset(StatuCellMargin).constraint
                })
            } else {
            
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //如果没有配图视图 底部约束就需要改成 相对于内同的底部约束
                    self.bottomConstraint =  make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatuCellMargin).constraint
                })

            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 0.9, alpha: 1)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI () {
    
          addSubview(retweetedLabel)
          addSubview(pictureView)
        
        //设置标签的约束
          retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(snp_top).offset(StatusCellMargin)
            make.left.equalTo(snp_left).offset(StatusCellMargin)
          }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(retweetedLabel.snp_left)
            //因为在这里我们需要设置图片的大小为自定义宽高
//            make.height.equalTo(100)
//            make.width.equalTo(100)
        }
        
        self.snp_makeConstraints { (make) -> Void in
           // make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
         self.bottomConstraint =   make.bottom.equalTo(pictureView.snp_bottom).offset(StatuCellMargin).constraint
            
        }
    }
    
    //设置转发微博
    private lazy var retweetedLabel: UILabel = UILabel(title: "我是转发微博", size: 14, color: UIColor.darkGrayColor(), margin: StatuCellMargin)
    
    //设置配图视图
    private lazy var pictureView: StatusPictureView = StatusPictureView()
}
